<?php
/**
 *
 * 版    本：1.1
 * 功能说明：文章控制器。
 *
 **/

namespace Admin\Controller;
use Vendor\Tree;

class ArticledController extends ComController
{

    public function add($pid=0)
    {
		$map['id']=$pid;
		$map['_string'] = "  concat(',',path,',') like '%,$pid,%'  ";
		$map['_logic'] = 'OR';
        $category = M('category')->where($map)->field('id,pid,name')->order('o asc')->select();
		//echo M('category')->getlastsql();die;
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, 0);
        $this->assign('category', $category);//导航
        $this->assign('pid', $pid);
        $ttt = time();
        $this->assign('ttt', $ttt);
        $this->display('form');
    }

    public function index($sid = 0, $p = 1,$pid=0)
    {


        $p = intval($p) > 0 ? $p : 1;

        $article = M('article');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $sid = isset($_GET['sid']) ? $_GET['sid'] : '';
        $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';

        if($pid){
			$map['id']=$pid;
			$map['_string'] = "  concat(',',path,',') like '%,$pid,%'  ";
			$map['_logic'] = 'OR';
			$data = M('category')->where($map)->select();
			foreach($data as $k=>$v){
				$class[]=$v['id'];  //获取所有分类ID
			}
            $path = implode(',',$class);
            $mapp['sid'] = array ('in',$class);

            $where = "sid in(".$path.")";

        }else{
            $where = '1 = 1 ';
        }
        
        if ($sid) {
            $sids_array = category_get_sons($sid);
            $sids = implode(',',$sids_array);
            $where .= "and {$prefix}article.sid in ($sids) ";
        }
        if ($keyword) {
            $where .= "and {$prefix}article.title like '%{$keyword}%' ";
        }
        //默认按照时间降序
        $orderby = "o desc, t desc,aid desc";
        if ($order == "asc") {

            $orderby = "o desc, t asc ,aid asc";
        }
        //获取栏目分类
        	 
        $category = M('category')->where($map)->order('o asc')->select();
        //echo M('category')->getLastsql();die;
        $tree = new Tree($category);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $category = $tree->get_tree(0, $str, $sid);
        $this->assign('category', $category);//导航

        $count = $article->where($where)->count();
        $list = $article->field("{$prefix}article.*,{$prefix}category.name")->where($where)->order($orderby)->join("{$prefix}category ON {$prefix}category.id = {$prefix}article.sid")->limit($offset . ',' . $pagesize)->select();
        //echo $article->getLastsql();die;
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('pid', $pid);
        $this->display();
    }

    public function del($pid=0)
    {

        $aids = isset($_REQUEST['aids']) ? $_REQUEST['aids'] : false;
        if ($aids) {
            if (is_array($aids)) {
                $aids = implode(',', $aids);
                $map['aid'] = array('in', $aids);

				$data = M('article')->where($map)->select();
				foreach($data as $k=>$v){
					if(file_exists('.'.$v['thumbnail'])){
						unlink('.'.$v['thumbnail']); //删除文件
					}
				}
            } else {
                $map = 'aid=' . $aids;
                
        		$data = M('article')->where($map)->find();
                //print_r('.'.$data['thumbnail']);die;
		        if(file_exists('.'.$data['thumbnail'])){
			        unlink('.'.$data['thumbnail']); //删除文件
		        }
            }

 

            if (M('article')->where($map)->delete()) {
                addlog('删除文章，AID：' . $aids);
                $this->success('恭喜，文章删除成功！',U('index',array('pid'=>$pid)));
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function edit($aid,$pid=0)
    {

        $aid = intval($aid);
        $article = M('article')->where('aid=' . $aid)->find();
        if ($article) {
			$map['id']=$pid;
			$map['_string'] = "  concat(',',path,',') like '%,$pid,%'  ";
			$map['_logic'] = 'OR';

            $category = M('category')->where($map)->field('id,pid,name')->order('o asc')->select();
            $tree = new Tree($category);
            $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
            $category = $tree->get_tree(0, $str, $article['sid']);
            $this->assign('category', $category);//导航

            $this->assign('article', $article);
            //print_r($article);die;
            $this->assign('pid', $pid);
        } else {
            $this->error('参数错误！');
        }


        $this->display('form');
    }

    public function update($act = null,$aid = 0,$pid=0,$sid=0)
    {
        if ($act == 'order') {
            $aid = I('post.aid', 0, 'intval');
            if (!$aid) {
                die('0');
            }
            $o = I('post.o', 0, 'intval');
            M('article')->data(array('o' => $o))->where("aid='{$aid}'")->save();
            addlog('分类修改排序，ID：' . $aid);
            die('1');
        }



        $aid = intval($aid);
        $data['sid'] = isset($_POST['sid']) ? intval($_POST['sid']) : 0;
        $data['title'] = isset($_POST['title']) ? $_POST['title'] : false;
        $data['seotitle'] = isset($_POST['seotitle']) ? $_POST['seotitle'] : '';
        $data['keywords'] = I('post.keywords', '', 'strip_tags');
        $data['description'] = I('post.description', '', 'strip_tags');
        $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
        $data['thumbnail'] = I('post.thumbnail', '', 'strip_tags');
        $data['o'] = I('post.o', '', 'strip_tags');
        $data['t'] = isset($_POST['t']) ? strtotime($_POST['t']) : 0;
        $data['did'] = I('post.did', '', 'strip_tags');
        //$data['t'] = time();
        if (!$data['sid'] or !$data['title'] or !$data['content']) {
           // $this->error('警告！文章分类、文章标题及文章内容为必填项目。');
        }
        if ($aid) {
			$list = M('category')->where('pid='.$sid)->find();
			if($list){
				$this->error('本栏目下有子栏目，请选择子栏目！','',1)	;
			}
            M('article')->data($data)->where('aid=' . $aid)->save();
            addlog('编辑文章，AID：' . $aid);
            $this->success('恭喜！文章编辑成功！',U('index',array('pid'=>$pid)));
        } else {
			$list = M('category')->where('pid='.$sid)->find();
			if($list){
				$this->error('本栏目下有子栏目，请选择子栏目！','',1)	;
			}
            $aid = M('article')->data($data)->add();
            if ($aid) {
                addlog('新增文章，AID：' . $aid);
                $this->success('恭喜！文章新增成功！',U('index',array('pid'=>$pid)));
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }

    public function did($aid=0)
    {
        $aid = intval($aid);
        $article1 = M('article')->where('aid=' . $aid)->find();
        $this->assign('article1',$article1);

        $did = M('category')->where('pid=46')->order('o desc,id asc')->select();
        foreach($did as $k=>$v){
            $did[$k]['did1']=M('article')->where('sid='.$v['id'])->order('o desc,aid desc')->select();
        }
        $this->assign('did',$did);

        $this->display('did');

    }
}